package cn.iocoder.yudao.module.custom.service.city;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.custom.controller.admin.city.vo.*;
import cn.iocoder.yudao.module.custom.dal.dataobject.city.CityDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.custom.convert.city.CityConvert;
import cn.iocoder.yudao.module.custom.dal.mysql.city.CityMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.custom.enums.ErrorCodeConstants.*;

/**
 * 城市信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CityServiceImpl implements CityService {

    @Resource
    private CityMapper cityMapper;

    @Override
    public String createCity(CityCreateReqVO createReqVO) {
        // 插入
        CityDO city = CityConvert.INSTANCE.convert(createReqVO);
        cityMapper.insert(city);
        // 返回
        return city.getId();
    }

    @Override
    public void updateCity(CityUpdateReqVO updateReqVO) {
        // 校验存在
        validateCityExists(updateReqVO.getId());
        // 更新
        CityDO updateObj = CityConvert.INSTANCE.convert(updateReqVO);
        cityMapper.updateById(updateObj);
    }

    @Override
    public void deleteCity(String id) {
        // 校验存在
        validateCityExists(id);
        // 删除
        cityMapper.deleteById(id);
    }

    private void validateCityExists(String id) {
        if (cityMapper.selectById(id) == null) {
            throw exception(CITY_NOT_EXISTS);
        }
    }

    @Override
    public CityDO getCity(String id) {
        return cityMapper.selectById(id);
    }

    @Override
    public List<CityDO> getCityList(Collection<String> ids) {
        return cityMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<CityDO> getCityPage(CityPageReqVO pageReqVO) {
        return cityMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CityDO> getCityList(CityExportReqVO exportReqVO) {
        return cityMapper.selectList(exportReqVO);
    }

    @Override
    public List<CityDO> getCityLists(Integer parentId) {
        return cityMapper.getCityLists(parentId);
    }

}
